#include <fstream>
#include "Mesh.hpp"
#include "SufFile.hpp"
using namespace std;

void SufFile::Write(const std::string& aFileName,
                    Mesh&              aMesh)
{
   ofstream file(aFileName.c_str());
   file << aMesh.verts.size() << ' ' << aMesh.faces.size() << '\n';
   for (size_t i = 0; i < aMesh.verts.size(); ++i)
   {
      Vec3& v = aMesh.verts[i];
      file << v.x << ' ' << v.y << ' ' << v.z << '\n';
   }
   for (size_t i = 0; i < aMesh.faces.size(); ++i)
   {
      Face& f = aMesh.faces[i];
      file << f.vertex[0] << ' ' << f.vertex[1] << ' ' << f.vertex[2] << ' '
         << f.materials[0] << ' ' << f.materials[1] << '\n';
   }
   file << '0';
}

void SufFile::Read(const std::string& aFileName,
                   Mesh&              aMesh)
{
   ifstream file(aFileName.c_str());
   size_t vertCount, faceCount;
   file >> vertCount >> faceCount;
   aMesh.verts->resize(vertCount);
   aMesh.faces->resize(faceCount);
   for (size_t i = 0; i < vertCount; ++i)
   {
      Vec3& v = aMesh.verts[i];
      file >> v.x >> v.y >> v.z;
   }
   for (size_t i = 0; i < faceCount; ++i)
   {
      Face& f = aMesh.faces[i];
      file >> f.vertex[0] >> f.vertex[1] >> f.vertex[2] 
         >> f.materials[0] >> f.materials[1];
   }
}
